/**
 * Created by henian.xu on 2018/9/18.
 * 解析
 */

const gonzales = require('gonzales-pe');

let parseTree = null;

export function parse(data) {
  parseTree = gonzales.parse(data, {syntax: 'scss'});

  parseTree.traverseByType('ruleset', (ruleset, index, parent) => {
    // console.log(node.contains('variable'))
    filterDeclaration(ruleset.first('block'));
  });
  console.log(JSON.stringify(parseTree.content));
  console.log(parseTree.toString());
  return parseTree.toString();
}

function filterDeclaration(block) {
  block.traverseByType('declaration', (declaration, index, parent) => {
    const value = declaration.first('value');
    if (value && value.contains('variable')) return;
    block.removeChild(index);
    if (block.get(index).is('declarationDelimiter')) {
      block.removeChild(index);
    }
  });
}